home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Games / MacGnuGo 0.5e / gnugo.src / randmove.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-28  |  2.4 KB  |  95 lines  |  [TEXT/R*ch]

  1. #include "comment.header"
  2.   
  3. #define EMPTY 0
  4. #define WHITESTONE 1
  5. #define BLACKSTONE 2
  6.  
  7. extern unsigned char p[19][19], l[19][19];
  8. extern int currentStone, opposingStone, MAXX, MAXY;
  9. extern long int rd;
  10. extern int lib, blackPassed, whitePassed;
  11. extern void countlib(int,int,int);
  12. extern int fioe(int,int);
  13. extern void uRandom(long int*);
  14. extern int blackCapturedKoI, blackCapturedKoJ,
  15.            whiteCapturedKoI, whiteCapturedKoJ;
  16. int fioe2(int,int,int);
  17.  
  18. void randmove(int *i, int *j, int me)
  19.      /* generate random move */
  20. {
  21.   int t, ti, tj;
  22.   int maxtry = 3*MAXX*MAXY;
  23.   int try;
  24.   int lib2;
  25.   int you;
  26.   int uik,ujk;
  27.   
  28.   /* initialize move  */
  29.   *i = -1;  *j = -1;
  30.   
  31.   if (me == opposingStone) you = currentStone;
  32.   else                     you = opposingStone;
  33.  
  34.   if (me == BLACKSTONE) {
  35.       uik = blackCapturedKoI;
  36.       ujk = blackCapturedKoJ;
  37.   } else {
  38.       uik = whiteCapturedKoI;
  39.       ujk = whiteCapturedKoJ;
  40.   }
  41.   
  42.   /* printf("ko : %c%d  \n", 'a' + ujk, MAXX - uik);
  43.     if (uik >= 0) getchar(); /* debug rhn xxx */
  44.     
  45.   eval(you);
  46.   
  47.   for (try = maxtry; try > 0; try--) {
  48.       uRandom(&rd);
  49.       t = (rd + *i + *j + 3) % (MAXX + 2);
  50.       uRandom(&rd);
  51.       *i = (t + rd) % MAXX;
  52.       uRandom(&rd);
  53.       *j = (t + rd) % MAXY;
  54.  
  55.       if (p[*i][*j] != EMPTY) continue;
  56.       if ((*i == uik) && (*j == ujk)) continue;
  57.  
  58.       lib = 0;
  59.       countlib(*i, *j, me);
  60.       lib2 = lib;
  61.  
  62.       if (lib < 2) {
  63.     if (*i > 0 && p[*i-1][*j] == you && l[*i-1][*j] == 1) { lib2 = 2; }
  64.     if (*i < MAXX-1 && p[*i+1][*j] == you && l[*i+1][*j] == 1) { lib2 = 2; }
  65.     if (*j > 0 && p[*i][*j-1] == you && l[*i][*j-1] == 1) { lib2 = 2; }
  66.     if (*j < MAXY-1 && p[*i][*j+1] == you && l[*i][*j+1] == 1) { lib2 = 2; }
  67.       }
  68.  
  69.       if ( (lib2 < 2) || fioe2(*i,*j,me) ) continue;
  70.       else break;
  71.    }
  72.    if (try < 2) *i = *j = -1;
  73.       /* avoid illegal move, liberty one or suicide, fill in own eye */
  74. /* printf("rand move: %c%-2d  %d  \n", 'a' + *j, MAXX - *i, maxtry-try); /* */
  75. }
  76.  
  77. int fioe2(int x,int y,int me)
  78. {
  79.   int i,j,a,n,m;
  80.   n = 0; m = 0; a = 0;
  81.   for (i = x-1; i < x+2; i++ ) for (j = y-1; j < y+2; j++) {
  82.     if (i < 0 || i > MAXX-1 || j < 0 || j > MAXY-1) continue;
  83.     else if (i == x && j == y) continue;
  84.     else if (p[i][j] != me) { m++; if (i == x || j == y) a++; }
  85.     else n++;
  86.   }
  87.   if (a > 0) return(0);
  88.   else if (n < 6) {
  89.     if (m > 0) return (0);
  90.     else return(1);
  91.   }
  92.   else return (1);
  93. }
  94. /* end randmove */
  95.